package com.woniu.tank.bullet;

import java.awt.Graphics;
import java.awt.Image;


import javax.swing.JPanel;

import com.woniu.tank.boom.Boom;
import com.woniu.tank.ui.GamePanel;

public class Bullet implements Runnable  {
	protected int x;
	protected int y;
	//定义子弹的方向0/1/2/3
	protected int dir;
	//定义子弹存活
	protected boolean live =true;
	//游戏结束
	private boolean isOver =true;
	//初始化画板
	GamePanel tc;


	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public Bullet() {
		new Thread(this).start();
	}

	public Bullet(int x, int y, int dir, GamePanel tc) {
		super();
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.tc = tc;
		new Thread(this).start();
	}

	//绘制出子弹
	public void drawBullet(Graphics g, JPanel p ,Image bullet){
		g.drawImage(bullet, x<<5, y<<5, (x+1)<<5, (y+1)<<5, -20, -20, 35, 35, p);
	}
	//	子弹的移动
	public void move() {		
		switch (dir){
		case 0:
			y--;
			break;
		case 1:
			x++;	
			break;
		case 2:
			y++;			
			break;
		case 3:
			x--;
			break;		 			
		}		
	}
	//子弹碰撞检测
	public void collision(int[][] map){
		switch(map[y][x]){
		case 2 :
			map[y][x] =0;
			live =false;
			break;
		case 1 :
			live = false;
			break;
		case 5 :			
			isOver = false;
			break;
		}	
	}
	@Override
	public void run() {
		while(live){
			move();
			collision(GamePanel.map);
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//添加爆炸效果
		GamePanel.boomtk.add(new Boom(x,y, tc));
		GamePanel.bbullet.remove(this);
		GamePanel.gbbullet.remove(this);
	}

}

